<template>
  <div id="table_container">
    <el-card style="margin-bottom: 5px; text-align: left">
      <el-input v-model="keyWord" placeholder="输入搜索内容" style="width: 200px;" @keyup.enter.native="handleFilter"></el-input>
      <el-select v-model="status" placeholder="状态" style="width: 90px;margin-left: 10px;">
        <el-option v-for="item in importanceOptions" :key="item.value" :label="item.label" :value="item.value"/>
      </el-select>
      <el-button type="primary" icon="el-icon-search" @click="handleFilter()" style="margin-left: 10px;">
        查询
      </el-button>
      <el-button  v-permission="['post:add']" class="create_btn" style="float: right;" type="primary" icon="el-icon-edit" @click="handleCreate">
        新增
      </el-button>
    </el-card>
    <el-card>
      <el-table
          :data="tableData"
          v-loading="loading"
          row-key="id"
          border>
        <el-table-column
            type="index"
            label="序号"
            align="center"
            width="50">
        </el-table-column>
        <el-table-column
            prop="name"
            label="岗位名称">
        </el-table-column>
        <el-table-column
            prop="key"
            label="岗位编码">
        </el-table-column>
        <el-table-column
            prop="sort"
            label="排序"
            align="center"
            width="50">
        </el-table-column>
        <el-table-column
            prop="status"
            label="状态"
            align="center"
            width="70">
          <template slot-scope="scope">
            <el-tag
                :type="scope.row.status === true ? 'success' :'danger' "
                disable-transitions>{{ scope.row.status_label }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
            label="更新时间"
            align="center"
            width="120">
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            <span style="margin-left: 10px">{{ scope.row.update_time }}</span>
          </template>
        </el-table-column>
        <el-table-column
            label="创建时间"
            align="center"
            width="120">
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            <span style="margin-left: 10px">{{ scope.row.create_time }}</span>
          </template>
        </el-table-column>
        <el-table-column
            fixed="right"
            width="180"
            align="center"
            label="操作">
          <template slot-scope="scope">
            <el-button v-permission="['post:edit']" @click="handleEdit(scope.row)" title="编辑" type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button v-permission="['post:delete']" @click="handleDelete(scope.row)" title="删除" type="danger" icon="el-icon-delete">删除</el-button>
          </template>
        </el-table-column>

      </el-table>
    </el-card>
    <el-dialog title="新建" width="400px"  @close="handleClose" :visible.sync="createDialogFormVisible">
      <el-form :model="form" style="text-align: left">
        <el-form-item label="岗位名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="岗位编码" :label-width="formLabelWidth">
          <el-input v-model="form.key" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="排序" :label-width="formLabelWidth">
          <el-input-number v-model="form.sort"></el-input-number>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-radio-group v-model="form.status">
            <el-radio :label="true">启用</el-radio>
            <el-radio :label="false">停用</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input
              type="textarea"
              :autosize="{ minRows: 4, maxRows: 8}"
              placeholder="请输入内容"
              v-model="form.description">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="createDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitCreate">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="编辑" width="400px"  @close="handleClose" :visible.sync="editDialogFormVisible" >
      <el-form :model="form" style="text-align: left">
        <el-form-item label="岗位名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="岗位编码" :label-width="formLabelWidth">
          <el-input v-model="form.key" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="排序" :label-width="formLabelWidth">
          <el-input-number v-model="form.sort"></el-input-number>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-radio-group v-model="form.status">
            <el-radio :label="true">启用</el-radio>
            <el-radio :label="false">停用</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input
              type="textarea"
              :autosize="{ minRows: 4, maxRows: 8}"
              placeholder="请输入内容"
              v-model="form.description">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitUpdate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {createPost, deletePost, getPost, updatePost} from "@/api";

export default {
  name: "index",
  data() {
    return {
      loading: true,
      tableData: [],

      createDialogFormVisible: false,
      showDialogFormVisible: false,
      editDialogFormVisible: false,
      form: {
        name: undefined,
        key: undefined,
        sort: 1,
        description: undefined,
        status: true,
      },
      formLabelWidth: '80px',
      keyWord: undefined,
      importanceOptions: [{label: '启用', value: 1}, {label: '停用', value: 0}],
      status: undefined,

      menuOptions: [],
    }
  },
  mounted() {
    this.getDataList()
  },
  methods: {
    getDataList(params) {
      getPost(params).then(res => {
        this.tableData = res.data
        setTimeout(() => {
          this.loading = false
        }, 1.5 * 500);
      })
    },
    handleClose() {
      this.form = {
        name: undefined,
        key: undefined,
        sort: 1,
        description: undefined,
        status: true,
      }
    },
    handleClick(row) {

    },
    handleFilter() {
      this.getDataList()
    },
    handleCreate() {
      this.createDialogFormVisible = true

    },
    handleDetail(row) {
      this.showDialogFormVisible = true
      this.loadDialogOptions()
      this.form = {...row}
    },
    handleEdit(row) {
      this.editDialogFormVisible = true
      this.form = {...row}
    },
    handleChange(row) {
      this.$router.push({name: 'menuButton', params: {menuId: row.id}})
    },
    handleDelete(row) {
      deletePost(row.id).then(res => {
        this.$message.success('删除成功！');
        // this.$router.go(0)
        this.getDataList()
      })

    },
    submitCreate() {
      let data = this.form
      createPost(data).then(res => {
        this.$message.success('新建成功！')
        this.createDialogFormVisible = false
        // this.$router.go(0)
        this.getDataList()
      })
    },
    submitUpdate() {
      let data = this.form
      updatePost(data).then(res => {
        this.$message.success('修改成功！')
        this.editDialogFormVisible = false
        // this.$router.go(0)
        this.getDataList()
      })
    },

  },
}
</script>

<style scoped>

</style>